package com.mainworks.template.service.security.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mainworks.commons.entity.User;
import com.mainworks.commons.impl.UserDetailed;
import com.mainworks.model.system.mapper.SysUserInfoMapper;
import com.mainworks.model.system.pojo.SysUserInfo;
import com.mainworks.template.service.security.SecurityUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * Security UserDetailsService 实现类
 * @Author: HuangMinJie
 * @CreateDate: 2021/10/31 12:34
 * @Description:
 * @Version: 1.0.0.0
 * @ModifiedBy:
 * @ModifiedDate:
 * @ModifiedDescription:
 */
@Service
public class UserDetailsServiceImpl implements SecurityUserDetailsService {
    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserDetailed userDetailed=null;
        QueryWrapper<SysUserInfo> query=new QueryWrapper();
        query.eq("user_name",username);
        SysUserInfo userInfo = sysUserInfoMapper.selectOne(query);

        if (userInfo!=null){
            userDetailed =new UserDetailed();
            User user =new User( userInfo.getId(),  userInfo.getOrgId(),  userInfo.getDepartmentId(),
                    userInfo.getRealName(),  userInfo.getUserName(),  userInfo.getNickname(),  userInfo.getCardNo(),
                    userInfo.getPasswordOverdue(),  userInfo.getEmail(),  userInfo.getMobilePhone(),  userInfo.getWechatOpenId(),
                    userInfo.getAlipayOpenId(),  userInfo.getIsSuperAdmin(),  userInfo.getIsExpired(),  userInfo.getIsLocked(),
                    userInfo.getIsEnabled(),userInfo.getPassword());
            userDetailed.setUser(user);
        }
        // 做权限处理
        return userDetailed;
    }
}
